<?php
  
namespace App\Utils;

use App\Services\Product\CRC16;

class Device
{
    /**
     * 格式化设备发送的数据
     * @param  [type] $data 
     * @return [type]
     */
    public static function dataFormat($data)
    {
        try{

           	$s = unpack('Vhead/vcommandStatus/vdataLength/vdataCommand/CdecOn/CdecStatus/CdecUV/vTDS1/vTDS2/CwaterTemperature/v5filterSet/v5filterCnt/VwaterCnt/VwaterTime', $data);

            $formatData = [];
            //起始标志命令字
            // $formatData['head'] = $s['head'];
            //命令返回状态
            // $formatData['commandStatus'] = $s['commandStatus'];
            //数据长度
            // $formatData['dataLength'] = $s['dataLength'];
            //命令字
            // $formatData['dataCommand'] = $s['dataCommand'];

            //设备码
            // $d1 = ($s["deviceCode1"]>>24)&255;//年
            // $d10 = ($s["deviceCode1"]>>16)&255;//月
            // $d11 = ($s["deviceCode1"]>>8)&255;//日
            // $d2 = $s["deviceCode1"]&255;//ID最低一个一字节取出
            // $d3 = ($s["deviceCode2"]<<8)|$d2;//组合为完整ID

            // //转为显示设备码形式
            // $formatData['deviceCode'] = substr(strval($d1 + 100), 1, 2);
            // $formatData['deviceCode'] .= substr(strval($d10 + 100), 1, 2);
            // $formatData['deviceCode'] .= substr(strval($d11 + 100), 1, 2);
            // $formatData['deviceCode'] .= substr(strval($d3 + 10000000000), 1, 10);
            
            $formatData['deviceCode'] = bin2hex(substr($data, strlen($data)-10, 6));

            // $formatData['decOn'] = $s["decOn"]; //开机状态
            // self::getDecOnDescription($formatData);

            $formatData['decStatus'] = $s["decStatus"] & 0x0f; //工作状态
            $formatData['decStatusDescription'] = config("konka.decStatus.$formatData[decStatus]");
            $formatData['effluentStatus'] = ($s["decStatus"] & 0xf0 ) >> 4; //工作状态
            $formatData['effluentStatusDescription'] = config("konka.effluentStatus.$formatData[effluentStatus]");

            // $formatData['decUV'] = $s["decUV"]; //制冷电源开关状态
            $formatData['TDS1'] = str_pad($s["TDS1"], 3,"0",STR_PAD_LEFT); //原水 TDS
            $formatData['TDS2'] = str_pad($s["TDS2"], 3,"0",STR_PAD_LEFT);; //纯水 TDS
            $formatData['waterTemperature'] = $s["waterTemperature"]; //水温
            //滤芯
            for ($i=1; $i<=2; $i++)
            {
                if($s["filterSet$i"] != 0)
                {
                    $formatData["filter$i"] = 100 - round($s["filterCnt$i"] / $s["filterSet$i"], 2) * 100;
                }
                else
                {
                    $formatData["filter$i"] = 0;
                }
                $formatData["filterCnt$i"] = floor($s["filterCnt$i"] * 0.28);
                $formatData["filterSet$i"] = $s["filterSet$i"];
            }

            //制水累计量
            $waterCnt = floor($s["waterTime"] * 0.28);
            $formatData['waterCnt' ]= str_pad($waterCnt, 6, "0", STR_PAD_LEFT);
            //制水累计时间
            $formatData['waterTime'] = $s["waterTime"];
            
            return $formatData;
        }catch(Exception $e){
            logger(['Device@dataFormat' => [
                'line' => $e->getLine(),
                'message' => $e->getMessage(),
                ]]);
        }
    }

    public static function getCmdByte($CON_En = 0, $CON_Ext = 0, $CON_Filter = 0, $deviceCode)
    {
        $binaryData  =  pack("v5C3", 0x00bb, 0x0006, 0x0000, 0x000D, 0x0002, $CON_En, $CON_Ext, $CON_Filter);
        $binaryData .= $deviceCode;
        $crc = CRC16::crc16(substr($binaryData, 4));
        $binaryData1 = pack("v",$crc);
        $binaryData.=$binaryData1;
        return $binaryData;
    }

}